<script lang="ts" setup name="XtxCity">
import { watch, ref, watchEffect } from 'vue'
import { onClickOutside } from '@vueuse/core'
import { IAreaType, AddressType } from './city'
import axios from 'axios'
defineProps<{
  address?: string
}>()
const emit = defineEmits<{
  (e: 'changeAddress', val: AddressType): void
}>()
// 1.控制下拉选择的显示与隐藏的布尔值
const active = ref(false)
// 2.点击元素之外隐藏
const target = ref(null)
// 2.1 点击城市区域之外的区域会触发回调函数
onClickOutside(target, () => {
  // 2.2 关闭弹层
  active.value = false
})
// (1数据不在自己的服务器上,2.将城市数据存储在当前组件中,3.原始axios发起请求)
// 3. 请求渲染城市数据 
const areaList = ref<IAreaType[]>([]) // 渲染的数据
const catchList = ref<IAreaType[]>([]) // 备份数据
const getAreaList = async () => {
  const res = await axios.get<IAreaType[]>('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
  areaList.value = res.data
  catchList.value = res.data
}
const AddressInfo = ref<AddressType>({
  // 省份的信息
  provinceName: '',
  provinceCode: '',
  // 市的信息
  cityName: '',
  cityCode: '',
  // 区/县的信息
  countyName: '',
  countyCode: ''
})
// 3.2 点击省份选择市/ 区 一层一层点击下去
const handleClick = (item: IAreaType) => {
  // 将点击的/省份/城市的areatList再赋值给 areatList
  // 5. 每点击一次都要记录对应省市区的名字+code
  // console.log(item)
  if (item.level === 0) {
    AddressInfo.value.provinceName = item.name
    AddressInfo.value.provinceCode = item.code
  }
  if (item.level === 1) {
    AddressInfo.value.cityName = item.name
    AddressInfo.value.cityCode = item.code
  }
  if (item.level === 2) {
    AddressInfo.value.countyName = item.name
    AddressInfo.value.countyCode = item.code
    active.value = false
    // 说明省市区选择完毕, 将完整的地址传递给父组件, 发送请求让父组件去发
    emit('changeAddress', AddressInfo.value)
  }
  areaList.value = item.areaList
}
getAreaList()

// 4. 解决 每次打开下拉菜单初始的数据, active为false, 直接将备份的数据恢复出来
/* watch(() => active.value, (newVal: boolean) => {
  if (!newVal) {
    areaList.value = catchList.value
  }
}) */
watchEffect(() => {
  if (!active.value) {
    areaList.value = catchList.value
  }
})
</script>
<template>
  <div class="xtx-city" ref="target">
    <div class="select" :class="{ active }" @click="active = !active">
      <span class="placeholder" v-if="!address">请选择配送地址</span>
      <span class="value" v-else>{{ address }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="active">
      <span class="ellipsis" v-for="item in areaList" :key="item.code" @click="handleClick(item)">{{ item.name }}</span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;

  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;

    &.active {
      background: #fff;
    }

    .placeholder {
      color: #999;
    }

    .value {
      color: #666;
      font-size: 12px;
    }

    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }

  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;

    >span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;

      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>